import wepy from 'wepy'
import { nearShops } from '@/api/request';

var QQMapWX = require('../qqmap-wx-jssdk.min.js');
var qqmapsdk
export default class underlineMixin extends wepy.mixin {
    pointToAddress(latitude, longitude, callback) {
        qqmapsdk.reverseGeocoder(
            {
                location: {
                    latitude: latitude,
                    longitude: longitude
                },
                success: function (res) {
                    callback(res.result.address_component.city);
                },
                fail: function (res) {
                    console.log(res);
                },
                complete: function (res) {
                    console.log(res)
                }
            }
        )
    }
    onLoad() {
        // console.log('onLoad')
        qqmapsdk = new QQMapWX({
            key: 'DXSBZ-HBSAU-HDHVJ-4TDWL-SBVIJ-N3FTV'
        });
    }
    onReady() {
        var _this = this;
        wx.getLocation({
            type: 'gcj02',
            altitude: true,
            success: function (res) {
                // console.log(res);
                let resdata=res;
                var latitude = res.latitude // 经度
                var longitude = res.longitude // 纬度
                wepy.setStorageSync('region', resdata);
                // wepy.setStorageSync('lng', res.longitude);
                // 根据坐标调用 pointToAddress 方法
                _this.pointToAddress(latitude, longitude, function (address) {
                    // 得到最终地址
                    console.log(address);
                    _this.county = address
                    _this.$apply()
                    // console.log( _this.$mixins[0].methods)
                    _this.$mixins[0].methods.dataNear(resdata.latitude,resdata.longitude,_this)
                })
            },
        })
    }
    methods = {
        async dataNear(lat,lng,_this){
            // console.log(lat,lng,_this)
            let resData = await nearShops({
                method: 'POST',
                data: {
                    lat:lat,
                    lng:lng
                },
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
                }
            })
            console.log(resData)
            _this.nearShops = resData.data.rows
            _this.$apply()
        }
    };
}

